﻿using System;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using LTJ.Config;
using LTJ.Common;
using LTJ.Common.XML;
using LTJ.Common.Generic;
using LTJ.Cache;
using LTJ.Entity;

namespace LTJ.FrameWork
{
    /// <summary>
    /// 缓存前台的一些界面HTML数据
    /// </summary>
    public class Caches
    {
        /// <summary>
        /// 获得禁止的ip列表
        /// </summary>
        /// <returns>禁止列表</returns>
        //public static List<IpInfo> GetBannedIpList()
        //{
        //    List<IpInfo> list = LTJ.Cache.SASCache.GetCacheService().RetrieveObject("/LTJ/BannedIp") as List<IpInfo>;

        //    if (list == null)
        //    {
        //        //list = Ips.GetBannedIpList();
        //        LTJ.Cache.SASCache.GetCacheService().AddObject("/LTJ/BannedIp", list);
        //    }
        //    return list;
        //}

        /// <summary>
        /// 获得用户后台菜单
        /// </summary>
        public static DataTable GetUserMenu(UserMenuType umt)
        {
            DataTable menutable = new DataTable();
            string cachekey = "";
            string nodekey = "";
            switch (umt)
            {
                case UserMenuType.TopMenu:
                    nodekey = "toptabmenu";
                    break;
                case UserMenuType.MainMenu:
                    nodekey = "mainmenu";
                    break;
                case UserMenuType.SubMenu:
                    nodekey = "submain";
                    break;
            }

            cachekey = "/LTJ/user_" + nodekey;
            menutable = SASCache.GetCacheService().RetrieveObject(cachekey) as DataTable;
            if (menutable == null || menutable.Rows.Count == 0)
            {
                string configPath = Utils.GetMapPath(BaseConfigs.GetSitePath.ToLower() + "config/usermenu.config");
                XmlDocumentExtender doc = new XmlDocumentExtender();
                doc.Load(configPath);
                menutable = new DataTable();
                menutable.Columns.Add("id");
                menutable.Columns.Add("title");
                menutable.Columns.Add("menuid");
                menutable.Columns.Add("parentid");
                menutable.Columns.Add("mainmenulist");
                menutable.Columns.Add("mainmenuidlist");
                menutable.Columns.Add("menutitle");
                menutable.Columns.Add("menulink");
                menutable.Columns.Add("pagename");
                menutable.Columns.Add("englishname");
                menutable.Columns.Add("frameid");
                menutable.Columns.Add("defaulturl");
                XmlNodeList Menus = doc.SelectNodes("/dataset/" + nodekey);

                foreach (XmlNode xmenu in Menus)
                {
                    DataRow dr = menutable.NewRow();
                    dr["id"] = xmenu["id"] == null ? "-1" : xmenu["id"].InnerText;
                    dr["title"] = xmenu["title"] == null ? "" : xmenu["title"].InnerText;
                    dr["menuid"] = xmenu["menuid"] == null ? "-1" : xmenu["menuid"].InnerText;
                    dr["parentid"] = xmenu["menuparentid"] == null ? "-1" : xmenu["menuparentid"].InnerText;
                    dr["mainmenulist"] = xmenu["mainmenulist"] == null ? "" : xmenu["mainmenulist"].InnerText;
                    dr["mainmenuidlist"] = xmenu["mainmenuidlist"] == null ? "" : xmenu["mainmenuidlist"].InnerText;
                    dr["menutitle"] = xmenu["menutitle"] == null ? "" : xmenu["menutitle"].InnerText;
                    dr["menulink"] = xmenu["link"] == null ? "" : xmenu["link"].InnerText;
                    dr["pagename"] = xmenu["pagename"] == null ? "" : xmenu["pagename"].InnerText;
                    dr["englishname"] = xmenu["englishname"] == null ? "" : xmenu["englishname"].InnerText;
                    dr["frameid"] = xmenu["frameid"] == null ? "" : xmenu["frameid"].InnerText;
                    dr["defaulturl"] = xmenu["defaulturl"] == null ? "" : xmenu["defaulturl"].InnerText;
                    menutable.Rows.Add(dr);
                }

                SASCache.GetCacheService().AddObject(cachekey, menutable);
            }

            return menutable;
        }
        /// <summary>
        /// 获得管理后台菜单
        /// </summary>
        public static DataTable GetAdminMenu(UserMenuType umt)
        {
            DataTable menutable = new DataTable();
            string cachekey = "";
            string nodekey = "";
            switch (umt)
            {
                case UserMenuType.TopMenu:
                    nodekey = "toptabmenu";
                    break;
                case UserMenuType.MainMenu:
                    nodekey = "mainmenu";
                    break;
                case UserMenuType.SubMenu:
                    nodekey = "submain";
                    break;
            }

            cachekey = "/LTJ/admin_" + nodekey;
            menutable = SASCache.GetCacheService().RetrieveObject(cachekey) as DataTable;
            if (menutable == null || menutable.Rows.Count == 0)
            {
                string configPath = Utils.GetMapPath(BaseConfigs.GetSitePath.ToLower() + "config/adminmenu.config");
                XmlDocumentExtender doc = new XmlDocumentExtender();
                doc.Load(configPath);
                menutable = new DataTable();
                menutable.Columns.Add("id");
                menutable.Columns.Add("title");
                menutable.Columns.Add("menuid");
                menutable.Columns.Add("parentid");
                menutable.Columns.Add("mainmenulist");
                menutable.Columns.Add("mainmenuidlist");
                menutable.Columns.Add("menutitle");
                menutable.Columns.Add("menulink");
                menutable.Columns.Add("pagename");
                menutable.Columns.Add("englishname");
                menutable.Columns.Add("frameid");
                menutable.Columns.Add("defaulturl");
                XmlNodeList Menus = doc.SelectNodes("/dataset/" + nodekey);

                foreach (XmlNode xmenu in Menus)
                {
                    DataRow dr = menutable.NewRow();
                    dr["id"] = xmenu["id"] == null ? "-1" : xmenu["id"].InnerText;
                    dr["title"] = xmenu["title"] == null ? "" : xmenu["title"].InnerText;
                    dr["menuid"] = xmenu["menuid"] == null ? "-1" : xmenu["menuid"].InnerText;
                    dr["parentid"] = xmenu["menuparentid"] == null ? "-1" : xmenu["menuparentid"].InnerText;
                    dr["mainmenulist"] = xmenu["mainmenulist"] == null ? "" : xmenu["mainmenulist"].InnerText;
                    dr["mainmenuidlist"] = xmenu["mainmenuidlist"] == null ? "" : xmenu["mainmenuidlist"].InnerText;
                    dr["menutitle"] = xmenu["menutitle"] == null ? "" : xmenu["menutitle"].InnerText;
                    dr["menulink"] = xmenu["link"] == null ? "" : xmenu["link"].InnerText;
                    dr["pagename"] = xmenu["pagename"] == null ? "" : xmenu["pagename"].InnerText;
                    dr["englishname"] = xmenu["englishname"] == null ? "" : xmenu["englishname"].InnerText;
                    dr["frameid"] = xmenu["frameid"] == null ? "" : xmenu["frameid"].InnerText;
                    dr["defaulturl"] = xmenu["defaulturl"] == null ? "" : xmenu["defaulturl"].InnerText;
                    menutable.Rows.Add(dr);
                }

                SASCache.GetCacheService().AddObject(cachekey, menutable);
            }
            return menutable;
        }

        /// <summary>
        /// 重置管理员菜单缓存
        /// </summary>
        /// <param name="umt"></param>
        public static void ResetAdminMenuCache(UserMenuType umt)
        {
            string cachekey = "";
            string nodekey = "";

            switch (umt)
            {
                case UserMenuType.TopMenu:
                    nodekey = "toptabmenu";
                    break;
                case UserMenuType.MainMenu:
                    nodekey = "mainmenu";
                    break;
                case UserMenuType.SubMenu:
                    nodekey = "submain";
                    break;
            }

            cachekey = "/LTJ/admin_" + nodekey;
            SASCache.GetCacheService().RemoveObject(cachekey);
        }

        /// <summary>
        /// 重置用户菜单缓存
        /// </summary>
        /// <param name="umt"></param>
        public static void ResetUserMenuCache(UserMenuType umt)
        {
            string cachekey = "";
            string nodekey = "";

            switch (umt)
            {
                case UserMenuType.TopMenu:
                    nodekey = "toptabmenu";
                    break;
                case UserMenuType.MainMenu:
                    nodekey = "mainmenu";
                    break;
                case UserMenuType.SubMenu:
                    nodekey = "submain";
                    break;
            }

            cachekey = "/LTJ/user_" + nodekey;
            SASCache.GetCacheService().RemoveObject(cachekey);
        }
    }
}
